
function generateOrder(orderNo) {
  var that = this
  //统一支付
  wx.showToast({
    title: '正在处理订单，请稍候...',
    icon: 'loading',
    duration: 10000,
    mask: true
  })
  // var login = wx.getStorageSync('login')
  // var orderidStorage = wx.getStorageSync('orderid')
  setTimeout(function () {
    wx.request({
      // url: `http://www.liangyan.cc/api/order/pay?XDEBUG_SESSION_START=PHPSTORM`,
      url: 'http://120.79.62.239/liangyan/public/index.php/api/order/pay?XDEBUG_SESSION_START=PHPSTORM',
      method: 'post',
      header: {
        'content-type': 'application/json',
        // 'appletId': wx.getStorageSync('appletId'),
        'appletId': '47cbcc3aef82744ba6ec00737ed1c8a7',
        'token': wx.getStorageSync('USER_TOKEN'),
        // 'token':'72b9a00c-ffa3-49ed-814b-3e8bb9557e35'
      },
      data: {
        order_no: orderNo,
        // session_3rd: login.session_3rd
      },
      success: function (res) {
        var pay = res.data;
        //发起支付
        //支付
        _payFn(pay);
      },
    })
  }, 2000)
}

function recharge(money) {
  var that = this
  //统一支付
  wx.showToast({
    title: '正在处理订单，请稍候...',
    icon: 'loading',
    duration: 10000,
    mask: true
  })
  // var login = wx.getStorageSync('login')
  // var orderidStorage = wx.getStorageSync('orderid')
  setTimeout(function () {
    wx.request({
      url: `http://chinwoeco.com/api/account/recharge?XDEBUG_SESSION_START=PHPSTORM`,
      method: 'post',
      header:{
        'token': '7da86c47122e85598adababd0521c01f'
      },
      data: {
        money: money,
        // session_3rd: login.session_3rd
      },
      success: function (res) {
        var pay = res.data;
        //发起支付
        //支付
        _payFn(pay);
      },
    })
  }, 2000)
}

/* 支付   */
function _payFn(param) {
  console.log(param, '发起支付');
  wx.hideToast();
  wx.requestPayment({
    timeStamp: param.timestamp,
    nonceStr: param.nonceStr,
    package: param.package,
    signType: param.signType,
    paySign: param.paySign,
    success: function (res) {
      wx.showModal({
        title: '提示',
        content: '支付成功',
        showCancel: false,
        success: function (res) {
          wx.removeStorageSync('check_addr');
          wx.removeStorageSync('check_spec');
          if (res.confirm) {
            wx.navigateTo({
              url: '../order/order',
            });
          }
        }
      })
      // wx.navigateBack({
      // delta: 1, // 回退前 delta(默认为1) 页面
      // success: function (res) {
      //     wx.showToast({
      //         title: '支付成功',
      //         icon: 'success',
      //         duration: 2000
      //     })
      // },
      // })
    },
    fail: function (res) {
      // fail
      console.log("支付失败")
      console.log(res)
      wx.showModal({
        title: '提示',
        content: '支付失败',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../order/order',
            });
          }
        }
      })
      return
    },
    complete: function () {
    }
  })
}
module.exports = {
  generateOrder: generateOrder,
  recharge: recharge
}